﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.USBKey
{
    /// <summary>
    /// USBKey_setup 的摘要说明。
    /// </summary>
    public partial class USBKey_setupPage : Page
    {
        //查看是否有USBKEY的参数,0有,1没有
        public static int DisplayType;

        #region 页面初始化

        protected override void OnInit(EventArgs e)
        {
            lbOnline.Click += lbOnline_Click;
            lbOffLine.Click += lbOffLine_Click;
            cmdUsR_key.Click += cmdUsR_key_Click;
            cmdNotUse_key.Click += cmdNotUse_key_Click;
            dbStaffList.ItemCreated += dbStaffList_ItemCreated;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            cb_selectAll.Attributes["onclick"] = "return selectAll();";
            cmdUsR_key.Attributes.Add("Onclick", "javascript:return confirm('是否让选中的人开始使用USB_Key？');");
            cmdNotUse_key.Attributes.Add("Onclick", "javascript:return confirm('是否让选中的人停止使用USB_Key？');");
            if (Request.QueryString["DisplayType"] != null)
            {
                if (Request.QueryString["DisplayType"] != "")
                {
                    DisplayType = Int32.Parse(Request.QueryString["DisplayType"]);
                }
                else
                {
                    DisplayType = 1;
                }
            }
            else
            {
                DisplayType = 1;
            }

            if (DisplayType == 1)
            {
                cmdNotUse_key.Visible = true;
                cmdUsR_key.Visible = false;
            }
            else
            {
                cmdNotUse_key.Visible = false;
                cmdUsR_key.Visible = true;
            }
            if (!Page.IsPostBack)
            {
                BindGrid();
            }

            base.OnLoad(e);
        }

        #endregion

        #region 遍历DataGrid获得checked的ID

        private IList<int> GetSelectedItemID(string controlID)
        {
            var list = new List<int>();
            //遍历DataGrid获得checked的ID
            foreach (DataGridItem item in dbStaffList.Items)
            {
                if (((CheckBox)item.FindControl(controlID)).Checked)
                {
                    list.Add(int.Parse(dbStaffList.DataKeys[item.ItemIndex].ToString()));
                }
            }
            return list.AsReadOnly();
        }

        #endregion

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dbStaffList.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        #endregion

        #region DataGrid数据绑定

        private void BindGrid()
        {
            var db = new Database();
            var prams = new[]{
                Database.MakeInParam("@StaffType", SqlDbType.Bit, 1, DisplayType)
            };

            DataTable dt;
            using (var dr = db.RunDataReader("dbo.sproc_GetStaffisneedkey", prams)) //存放人物的数据
            {
                dt = Tools.ConvertDataReaderToDataTable(dr);
            }
            dbStaffList.DataSource = dt.DefaultView;
            dbStaffList.DataBind();
        }

        #endregion

        #region 选择使用KEY的员工

        private void lbOnline_Click(object sender, EventArgs e)
        {
            Server.Transfer("USBKey_setup.aspx?DisplayType=0");
        }

        #endregion

        #region 选择不使用KEY的员工

        private void lbOffLine_Click(object sender, EventArgs e)
        {
            Server.Transfer("USBKey_setup.aspx?DisplayType=1");
        }

        #endregion

        #region 取消使用KEY

        private void cmdNotUse_key_Click(object sender, EventArgs e)
        {
            var list = GetSelectedItemID("Staff_ID");
            if (list.Count > 0)
            {
                var person = new Components.Staff();
                if (person.SetIsNeedKey(list, false))
                {
                    BindGrid();
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('您没有选择任何人员！');</script>");
            }
        }

        #endregion

        #region 使用KEY

        private void cmdUsR_key_Click(object sender, EventArgs e)
        {
            var list = GetSelectedItemID("Staff_ID");
            if (list.Count > 0)
            {
                var person = new Components.Staff();
                if (person.SetIsNeedKey(list, true))
                {
                    //DisplayType=0;
                    BindGrid();
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('您没有选择任何人员！');</script>");
            }
        }

        #endregion

        #region 可以手动改变DataGrid的列宽

        private void dbStaffList_ItemCreated(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Header)
            {
                if (true) //如果允许改变列宽
                {
                    for (int i = 0; i < e.Item.Cells.Count; i++)
                    {
                        e.Item.Cells[i].Attributes.Add("onmousemove", "SyDG_moveOnTd(this)");
                        e.Item.Cells[i].Attributes.Add("onmousedown", "SyDG_downOnTd(this)");
                        e.Item.Cells[i].Attributes.Add("onmouseup", "this.mouseDown=false");
                        e.Item.Cells[i].Attributes.Add("onmouseout", "this.mouseDown=false");
                    }
                }
            }
        }

        #endregion
    }
}